using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using ADODB;
using Microsoft.CommerceServer.Interop;
using Microsoft.CommerceServer.Interop.Configuration;
using Microsoft.CommerceServer.Interop.Profiles;
using MSXMLLib;

namespace SiteTermViewer
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cmbSites;
		private System.Windows.Forms.Label lblSiteName;
		private System.Windows.Forms.Button btnGetSiteTerm;
		private System.Windows.Forms.Panel pnlTop;

		private MSXMLLib.DOMDocument xmlSiteTerms;
		private System.Windows.Forms.Panel pnlRight;
		private System.Windows.Forms.TreeView tvSiteTerms;
		private string sConstr;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBox4;
		private XMLNode xmlnode;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cmbSites = new System.Windows.Forms.ComboBox();
			this.lblSiteName = new System.Windows.Forms.Label();
			this.btnGetSiteTerm = new System.Windows.Forms.Button();
			this.tvSiteTerms = new System.Windows.Forms.TreeView();
			this.pnlTop = new System.Windows.Forms.Panel();
			this.pnlRight = new System.Windows.Forms.Panel();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.pnlTop.SuspendLayout();
			this.pnlRight.SuspendLayout();
			this.SuspendLayout();
			// 
			// cmbSites
			// 
			this.cmbSites.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbSites.Location = new System.Drawing.Point(80, 8);
			this.cmbSites.Name = "cmbSites";
			this.cmbSites.Size = new System.Drawing.Size(304, 21);
			this.cmbSites.TabIndex = 0;
			// 
			// lblSiteName
			// 
			this.lblSiteName.Location = new System.Drawing.Point(8, 8);
			this.lblSiteName.Name = "lblSiteName";
			this.lblSiteName.Size = new System.Drawing.Size(64, 16);
			this.lblSiteName.TabIndex = 1;
			this.lblSiteName.Text = "Site Name:";
			// 
			// btnGetSiteTerm
			// 
			this.btnGetSiteTerm.Location = new System.Drawing.Point(392, 8);
			this.btnGetSiteTerm.Name = "btnGetSiteTerm";
			this.btnGetSiteTerm.Size = new System.Drawing.Size(152, 24);
			this.btnGetSiteTerm.TabIndex = 2;
			this.btnGetSiteTerm.Text = "Get Site Term";
			this.btnGetSiteTerm.Click += new System.EventHandler(this.btnGetSiteTerm_Click);
			// 
			// tvSiteTerms
			// 
			this.tvSiteTerms.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tvSiteTerms.ImageIndex = -1;
			this.tvSiteTerms.Location = new System.Drawing.Point(0, 0);
			this.tvSiteTerms.Name = "tvSiteTerms";
			this.tvSiteTerms.SelectedImageIndex = -1;
			this.tvSiteTerms.Size = new System.Drawing.Size(232, 358);
			this.tvSiteTerms.TabIndex = 3;
			this.tvSiteTerms.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvSiteTerms_AfterSelect);
			// 
			// pnlTop
			// 
			this.pnlTop.Controls.Add(this.cmbSites);
			this.pnlTop.Controls.Add(this.btnGetSiteTerm);
			this.pnlTop.Controls.Add(this.lblSiteName);
			this.pnlTop.Dock = System.Windows.Forms.DockStyle.Top;
			this.pnlTop.Location = new System.Drawing.Point(0, 0);
			this.pnlTop.Name = "pnlTop";
			this.pnlTop.Size = new System.Drawing.Size(554, 32);
			this.pnlTop.TabIndex = 4;
			// 
			// pnlRight
			// 
			this.pnlRight.Controls.Add(this.tvSiteTerms);
			this.pnlRight.Dock = System.Windows.Forms.DockStyle.Left;
			this.pnlRight.Location = new System.Drawing.Point(0, 32);
			this.pnlRight.Name = "pnlRight";
			this.pnlRight.Size = new System.Drawing.Size(232, 358);
			this.pnlRight.TabIndex = 5;
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(352, 88);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(192, 20);
			this.textBox3.TabIndex = 19;
			this.textBox3.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(352, 64);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(192, 20);
			this.textBox2.TabIndex = 18;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(352, 40);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(192, 20);
			this.textBox1.TabIndex = 17;
			this.textBox1.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(240, 88);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 16);
			this.label3.TabIndex = 16;
			this.label3.Text = "Description:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(240, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 15;
			this.label2.Text = "Display Name:";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(240, 40);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 16);
			this.label1.TabIndex = 14;
			this.label1.Text = "Name:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(240, 120);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(304, 23);
			this.label4.TabIndex = 21;
			this.label4.Text = "Debug Data";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(240, 152);
			this.textBox4.Multiline = true;
			this.textBox4.Name = "textBox4";
			this.textBox4.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox4.Size = new System.Drawing.Size(304, 232);
			this.textBox4.TabIndex = 20;
			this.textBox4.Text = "";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(554, 390);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.pnlRight);
			this.Controls.Add(this.pnlTop);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "Site Term Viewer";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.pnlTop.ResumeLayout(false);
			this.pnlRight.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			Displaysites();
		}

		/// <summary>
		/// Display all Commerce Site.
		/// </summary>
		/// <example>
		///<code lang="C#">Displaysites();</code>
		///</example>
		private void Displaysites()
		{
			try
			{
				ArrayList CSSites = new ArrayList();
				GlobalConfig oGlbCfg = new GlobalConfigClass();

				// must be an admin to access the resources
				oGlbCfg.Initialize(null);

				// clear the combo
				cmbSites.Items.Clear();

				foreach(Field fld in oGlbCfg.Sites().Fields)
				{
					cmbSites.Items.Add(fld.Value.ToString());
				}
			
				cmbSites.SelectedIndex = 0;
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		/// <summary>
		/// Display all site terms of a selected Commerce Site.
		/// </summary>
		/// <example>
		///<code lang="C#">GetSiteTerms();</code>
		///</example>
		private void GetSiteTerms()
		{
			// get the connection string for our BizAdmin store
			SiteConfigReadOnly siteCfg = new SiteConfigReadOnlyClass();
			BusinessDataAdminFreeThreaded BizAdmin = new BusinessDataAdminFreeThreaded();
			xmlSiteTerms = new MSXMLLib.DOMDocumentClass();

			siteCfg.Initialize(cmbSites.Text);
			
			sConstr = ((Recordset)siteCfg.Fields["Biz Data Service"].Value).Fields["s_BizDataStoreConnectionString"].Value.ToString();
			string dummy = null;
			string ProfilePath = "Site Terms.MSCommerce";
			// Connect to the store
			BizAdmin.Connect(ref sConstr,ref dummy, ref dummy);
			xmlSiteTerms = BizAdmin.GetProfile(ref ProfilePath, "");
		}

		private void load_tree()
		{
			XMLNode xnode = new XMLNode();

			// clear the tree
			tvSiteTerms.Nodes.Clear();
			tvSiteTerms.Nodes.Add(new XMLNode("Site Terms for "+cmbSites.Text));
			xnode = (XMLNode)tvSiteTerms.Nodes[0];
			tree_walk(xmlSiteTerms.selectNodes(@"Document/Catalog/Profile/Group"), xnode);
		}

		private void tree_walk(IXMLDOMNodeList doc, XMLNode node)
		{
			for (int current=0; current<doc.length; current++) 
			{
				IXMLDOMNode child=doc[current];

				if (child.nodeType==DOMNodeType.NODE_ELEMENT) 
				{
					int i = node.Nodes.Add(new XMLNode(child));
					XMLNode tNode = (XMLNode)node.Nodes[i];
					
					if (child.hasChildNodes())
					{	
						tree_walk(child.childNodes, tNode);
					}
					doc.nextNode();
				}
			}
		}

		private void btnGetSiteTerm_Click(object sender, System.EventArgs e)
		{
			GetSiteTerms();
			load_tree();		
		}

		private void tvSiteTerms_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			textBox1.Text = "";
			textBox2.Text = "";
			textBox3.Text = "";
			textBox4.Text = "";

			// make sure we have something to work with
			xmlnode = (XMLNode)e.Node;
			if(xmlnode.m_XMLNode!=null)
			{
				textBox4.Text = ((XMLNode)e.Node).m_XMLNode.xml;
				IXMLDOMNode mnode = ((XMLNode)e.Node).m_XMLNode;

				for(int i=0; xmlnode.m_XMLNode.attributes.length > i; i++)
				{
					switch(xmlnode.m_XMLNode.attributes[i].nodeName)
					{
						case "name":
							textBox1.Text = xmlnode.m_XMLNode.attributes.getNamedItem("name").text;
							break;
						case "displayName":
							textBox2.Text = xmlnode.m_XMLNode.attributes.getNamedItem("displayName").text;
							break;
						case "description":
							textBox3.Text = xmlnode.m_XMLNode.attributes.getNamedItem("description").text;
							break;
					}
				}
			}
		}
	}
}
